package com.zzyk.web.controller;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.zzyk.main.configuration.aspectj.annotation.OperaLog;
import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.controller.BaseController;
import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.EnterpriseTimeRule;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.model.vo.OrganizationInfo;
import com.zzyk.main.model.vo.TimeRuleVO;
import com.zzyk.main.service.TimeRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wss_PC
 * @DESCRIPTION
 * @create 2020-6-29
 */
@RestController
@RequestMapping("/timeRule")
public class TimeRuleController extends BaseController {
    @Autowired
    private TimeRuleService timeRuleService;

    /**
     * 根据企业ID查询企业下所有的时间规则
     * @param eId
     * @return
     */
    @GetMapping("/getTimeRuleByEnterprise")
    public  List<TimeRuleVO> getTimeRuleByEnterprise(Integer eId){
        return timeRuleService.findTimeRuleByEnterprise(eId);
    }
    /**
     * 删除时间规则
     * @param id
     * @return
     */
    @OperaLog("删除企业时间规则")
    @GetMapping("/deleteInfo")
    public Message deleteInfo(Integer id){
        try {
            timeRuleService.deleteByEnterprise(id);
            return Message.success();
        }catch (Exception e){
            e.printStackTrace();
            return Message.failed("删除失败");
        }
    }


    @RequestMapping("/getTimeRuleByParam")
    public Message ruleList(OrganizationInfo organizationInfo) throws UserException {
        SysUser user = currentUser();
        String[] area = user.getArea();
        organizationInfo.setArea(area);
        setPage();
        List<EnterpriseTimeRule> list = timeRuleService.findTimeRuleByParam(organizationInfo);
        PageInfo<EnterpriseTimeRule> info = new PageInfo<>(list);
        return Message.success()
                .addData("data", info.getList())
                .addData("total", info.getTotal())
                .addData("pages", info.getPages());
    }

    /**
     * 保存时间规则
     * @param eId
     * @param beginTime
     * @param endTime
     * @param cycle
     * @return
     */
    @OperaLog("操作时间规则")
    @PostMapping("/saveOrUpdateTimeRule")
    @Transactional(rollbackFor = Exception.class)
    public Message saveOrUpdateTimeRule(Integer eId,String[] beginTime,String[] endTime,String[] cycle,Integer methodType,Integer eIdT){
        try {
            if(methodType==1){
                timeRuleService.deleteByEnterprise(eIdT);
                if(ObjectUtil.isNotNull(beginTime)&&beginTime.length>0){
                    timeRuleService.addTimeRule(eIdT,beginTime,endTime,cycle);
                }
                return Message.success();
            }
           timeRuleService.addTimeRule(eId,beginTime,endTime,cycle);
            return Message.success();
        }catch (Exception e){
            e.printStackTrace();
            return Message.failed("保存失败");
        }
    }
}